package activity

import (
	"lao-a/controllers"
	"lao-a/helper"
	act "lao-a/models/activity"
	"lao-a/models/model"
	"net/http"
	"strconv"
)

// ActivityController 应用
type ActivityController struct {
	dao *act.DAO
	controllers.BaseController
}

// Constructor 初始化 Controller
// @Title Constructor
// @Description 初始化 Controller, 系统自动调用
func (c *ActivityController) Constructor() {
	c.dao = act.NewDAO(c.Context)
}

// List 查询活动列表
func (c *ActivityController) ActivityList() {
	page := c.GetString("page")

	limit := []string{
		page,
		strconv.Itoa(controllers.PAGENUM),
	}
	acts, err := c.dao.GetActivityList(nil, limit)

	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson(map[string]interface{}{
		"Activities": acts,
	})
}

// Get 查询活动
func (c *ActivityController) GetActivity() {
	actId, _ := c.GetInt(":actId")
	actDetail, err := c.dao.GetActivityById(actId)

	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson(map[string]interface{}{
		"Activity": actDetail,
	})
}

// GetActivityTypes 查询活动分类字典
func (c *ActivityController) GetActivityTypes() {
	types, err := c.dao.GetActivityTypes()

	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson(map[string]interface{}{
		"ActivityTypes": types,
	})
}

// Update
func (c *ActivityController) UpdateActivity() {
	actDetail := model.TaActivity{}
	if err := c.ParseForm(&actDetail); err != nil {
		c.ResponseJson("接受参数失败", http.StatusInternalServerError)
	}

	actDetail.ActivityId, _ = c.GetInt(":actId")

	cols := []string{
		"title",
		"tags",
		"status",
		"location",
		"special",
		"basic_info",
		"travel_plan",
		"about_fee",
		"summary",
		"cover",
		"fee_brief",
		"start_date",
		"end_date",
		"start_book_date",
		"end_book_date",
		"contact",
		"to_index",
		"to_slider",
	}

	if err := c.dao.UpdateActivity(&actDetail, cols); err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson("ok")
}

// Save 保存活动
func (c *ActivityController) SaveActivity() {
	actDetail := &model.TaActivity{}
	err := c.ParseForm(actDetail)
	if err != nil {
		c.ResponseJson(
			helper.LogError("接受参数失败", err),
			http.StatusInternalServerError,
		)
	}

	// actDetail.Status = act.ACTIVITY_READY

	actDetail, err = c.dao.SaveActivity(actDetail)
	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson(map[string]interface{}{
		"Activity": actDetail,
	})
}
